import 'dart:collection';
import 'dart:math';
import 'dart:ui';

import 'package:animation_list/animation_list.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:zego_express_screen_capture/xx/srt_transform.dart';

class AAA extends StatefulWidget {
  const AAA({Key? key, required this.list, required this.title, this.index, this.subTitle}) : super(key: key);

  final List<SrtModel> list;
  final String title;
  final String? index;
  final String? subTitle;

  @override
  _AAAState createState() => _AAAState();
}

enum AStyle { normal, left, right }

class _AAAState extends State<AAA> {
  late Ticker _ticker;

  int nowTime = -1;

  List<SrtModel> start = [];
  List<SrtModel> end = [];

  final int size = 4;

  final double width = 140;
  final double height = 120;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _ticker.stop(canceled: true);
  }

  @override
  void initState() {
    super.initState();
    _ticker = Ticker((Duration duration) {
      print("printXXXXXXXXx");
      int tempTime = 10000000000 - duration.inMilliseconds;

      // if (tempTime <= 0) {
      //   ///倒计时结束
      //   _ticker.stop(canceled: true);
      //
      //   print("倒计时结束");
      //
      //   return;
      // }

      handleXX(duration);
    });

    // _ticker.start();
  }

  int index = 0;

  AStyle style = AStyle.normal;

  int innerIndex = 0;

  handleXX(Duration duration) {
    if (index > widget.list.length) {
      return;
    }
    SrtModel currentModel = widget.list[index];

    if (duration.inMilliseconds <= currentModel.startTime!) {
      return;
    }

    /// 倒计时每帧都会回调  减少回调
    if (duration.inMilliseconds <= currentModel.endTime!) {
      nowTime = duration.inMilliseconds;

      // int value = index % 4;
      int value = (innerIndex / 4).floor();

      if (value >= 4) {
        innerIndex = 0;
      }

      index++;
      innerIndex++;
      if (value == 0 || value == 2) {
        style = AStyle.normal;
      } else if (value == 1) {
        style = AStyle.left;
      } else {
        style = AStyle.right;
      }

      start.add(currentModel);
      if (start.length >= 4) {
        end.add(start.first);
        start.removeAt(0);
      }

      if (end.length >= 3) {
        end.removeAt(0);
      }
      print("start:" + start.toString());
      print("end:" + end.toString());

      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    final Widget viewTop = buildView1();
    final Widget viewLeft = buildView1();
    final Widget viewRight = buildView1();
    final Widget view2 = buildView2();
    SizedBox sizedBox = SizedBox(width: width, height: width);

    return Scaffold(
      appBar: AppBar(),
      body: Column(
        children: [
          MaterialButton(
              onPressed: () {
                _ticker.start();
                // handleXXx();
              },
              child: Text(
                "开始动画",
                style: TextStyle(fontSize: 14),
              )),
          SizedBox(
            height: 30,
          ),
          Stack(
            children: [
              Container(
                  height: 520,
                  width: 420,
                  color: Colors.black,
                  child: Column(
                    children: [
                      SizedBox(height: 60),


                      Row(
                        children: [
                          SizedBox(height: width, width: height),
                          AStyle.normal == style ? viewTop : sizedBox,
                          SizedBox(height: width, width: height),
                        ],
                      ),
                      Row(
                        children: [
                          AStyle.left == style
                              ? viewLeft
                              : SizedBox(height: width, width: height),
                          view2,
                          AStyle.right == style
                              ? viewRight
                              : SizedBox(height: width, width: height),
                        ],
                      )
                    ],
                  )),

              Positioned(child: Image.asset("lib/images/talkshow.jpg",width: 130,height: 130,),top: 10,right: 10,),
              Positioned(child:      Center(
                child: Column(
                  children: [
                    Text(
                      widget.title,
                      style: TextStyle(
                          fontSize: 30,
                          fontWeight: FontWeight.w500,
                          color: Colors.yellow,
                          fontFamily: "Pmzdzt"),
                    ),

                    Text(
                      widget.index??"",
                      style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.w400,
                          color: Colors.white,
                          fontFamily: "Pmzdzt"),
                    ),

                    Text(
                      widget.subTitle??"",
                      style: TextStyle(
                          fontSize: 35,
                          fontWeight: FontWeight.w500,
                          color: Colors.white,
                          fontFamily: "Pmzdzt"),
                    ),
                  ],
                ),
              ),top: 20,left: 0,right: 0,),


            ],
          ),
        ],
      ),
    );
  }

  Widget buildView2() {
    return Container(
        height: width,
        width: width,
        // color: Colors.blue,
        child: ListView.builder(

          itemBuilder: (BuildContext context, int indexX) {
            return _buildText(start[indexX], false,isFirst: indexX==start.length-1);
          },
          itemCount: start.length,
          shrinkWrap: true,
        ));
  }

  double getAngle() {
    if (AStyle.normal == style) {
      return 0;
    }
    if (AStyle.left == style) {
      return -pi / 2;
    }
    if (AStyle.right == style) {
      return pi / 2;
    }

    return 0;
  }

  Widget buildView1() {
    return Transform.rotate(

        ///左边-pi/2    右边pi/2  上面0
        angle: getAngle(),
        child: Container(
            height: height,
            width: width,
            // color: Colors.red,
            alignment: Alignment.bottomCenter,
            child: Column(
              children: [
                Spacer(),
                ListView.builder(
                  itemBuilder: (BuildContext context, int indexX) {
                    return _buildText(end[indexX], true);
                  },
                  itemCount: end.length,
                  shrinkWrap: true,
                ),
              ],
            )));
  }

  Widget _buildText(SrtModel model, bool isEnd,{bool isFirst=false}) {
    String text = model.content ?? "";

    double size = model.fontSize;
    if (text.length > 6) {
      size = model.fontSize - 1;
    }

    if (text.length > 8) {
      size = model.fontSize - 2;
    }
    if (text.length > 10) {
      size = model.fontSize - 4;
    }
    if (text.length > 12) {
      size = model.fontSize - 5;
    }
    return Text(
      text,
      style: TextStyle(
          fontSize: isFirst?20:(size + (isEnd ? 0 : 3).toDouble()),
          fontWeight: FontWeight.w500,
          color: model.color,
          fontFamily: "Pmzdzt"),
    );
  }
}
